<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
      <el-form-item label="公司名称" prop="companyName">
        <el-input v-model="form.companyName" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="合作伙伴类型" prop="partnerType">
        <el-select v-model="form.partnerType" placeholder="请选择合作伙伴类型" filterable style="width: 370px;">
          <el-option v-for="item in dict.ict_partner_type" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="公司所在地" prop="companyLocation">
        <el-input v-model="form.companyLocation" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="省份" prop="province">
        <el-select v-model="form.province" placeholder="请选择省份" filterable style="width: 370px;">
          <el-option v-for="item in dict.ict_partner_province" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="公司简介" prop="introduction">
        <el-input v-model="form.introduction" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="公司注册时间" prop="registrationDate">
        <el-input v-model="form.registrationDate" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="公司员工数" prop="employeeCount">
        <el-input v-model="form.employeeCount" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="公司近一年销售额" prop="oneYearSales">
        <el-input v-model="form.oneYearSales" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="联系人姓名" prop="contactName">
        <el-input v-model="form.contactName" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="联系人职务" prop="contactPosition">
        <el-input v-model="form.contactPosition" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="联系人电话" prop="contactNumber">
        <el-input v-model="form.contactNumber" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="联系人邮箱" prop="contactMailbox">
        <el-input v-model="form.contactMailbox" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="公司网址" prop="companyWebsite">
        <el-input v-model="form.companyWebsite" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="公司介绍材料" prop="companyIntroductionMaterials">
        <el-input v-model="form.companyIntroductionMaterials" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="备注信息">
        <el-input v-model="form.remarks" style="width: 370px;" />
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="cancelForm()">取消</el-button>
        <el-button :loading="loading" type="primary" @click="submitForm(form)">确认</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import crudIctPartner from '@/api/ict/ictPartner/ictPartner'
import CRUD from '@crud/crud'

const defaultForm = { id: null, companyName: null, companyLocation: null, province: null, introduction: null, registrationDate: null, employeeCount: null, oneYearSales: null, contactName: null, contactPosition: null, contactNumber: null, contactMailbox: null, companyWebsite: null, companyIntroductionMaterials: null, createBy: null, createDate: null, updateBy: null, updateDate: null, remarks: null, delFlag: null }
export default {
  name: 'IctPartnerForm',
  dicts: ['ict_partner_province', 'ict_partner_type'],
  data() {
    return {
      loading: false,
      form: {},
      id: this.$route.query.id,
      permission: {
        add: ['admin', 'ictPartner:add'],
        edit: ['admin', 'ictPartner:edit'],
        del: ['admin', 'ictPartner:del']
      },
      rules: {
        id: [
          { required: true, message: '合作伙伴ID不能为空', trigger: 'blur' }
        ],
        companyName: [
          { required: true, message: '公司名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    if (this.id) {
      this.getDetail(this.id)
    } else {
      this.initForm()
    }
  },
  methods: {
    cancelForm() {
      this.$router.go(-1)
    },
    submitForm(form) {
      this.loading = true
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.id) {
            this.toEdit(form)
          } else {
            this.toAdd(form)
          }
        } else {
          console.log('error submit!')
          return false;
        }
      })      
      this.loading = false
    },
    toAdd(form) {
      crudIctPartner.add(form).then(res => {
          if (res.msg === 'OK') {
            this.$notify.success({
              title: '添加成功'
            })
            this.$router.go(-1)
          }
        }).catch(e => {
          this.$notify.error({
            title: '添加失败' + e
          })
        })
    },
    toEdit(form) {
      crudIctPartner.edit(form).then(res => {
          if (res.msg === 'OK') {
            this.loading = false
            this.$notify.success({
              title: '更新成功'
            })
            this.$router.go(-1)
          }
        }).catch(e => {
          this.$notify.error({
            title: '更新失败' + e
          })
        })
    },
    getDetail(id) {
      crudIctPartner.get(id).then(res => {
        this.form = res.data
      }).catch(e => {
        console.log(e)
      })
    },
    initForm() {
      this.form = defaultForm
      console.log(this.form)
    },
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 获取数据后对数据进行二次处理
    [CRUD.HOOK.afterRefresh]() {

    }
  }

}
</script>

<style lang="scss" scoped>

</style>
